package com.stlm2.dems.service;

import com.stlm2.dems.dao.SpecificationDao;
import com.stlm2.dems.entity.Specification;
import com.stlm2.dems.entity.User;
import com.stlm2.util.Convert;
import com.stlm2.util.codecontent.CodeContent;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.SpecificationContent;
import com.stlm2.dems.dao.SpecificationContentDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.util.*;

@Service("specificationContentService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class SpecificationContentService extends BaseServiceImpl<SpecificationContent> {

	@Resource
	private SpecificationContentDao specificationContentDao;
	@Resource
	private SpecificationService specificationService;
	@Resource
	private UserService userService;
	@Resource
	private SpecificationContentService specificationContentService;

	@Override
	protected BaseDao <SpecificationContent> getDao() {
		return specificationContentDao;
	}

    public List<SpecificationContent> getSpecificationContent(Integer goodid, Integer specificationid) {
		return specificationContentDao.getSpecificationContent(goodid,specificationid);
    }

	public List<ModelMap> convent(List<SpecificationContent> specificationContents) {
		List<ModelMap> list=new ArrayList<ModelMap>();
		for(SpecificationContent sc:specificationContents){
			list.add(builderModel(sc));
		}
		return list;
	}

	private ModelMap builderModel(SpecificationContent sc) {
		ModelMap map=new ModelMap();
		map.put("id",sc.getId());
		map.put("content",sc.getContent());
		return map;
	}

	public List<SpecificationContent> getSpecificationContentAll(Integer goodid) {
		return specificationContentDao.getSpecificationContentAll(goodid);
	}

	public List<ModelMap> conventAll(List<SpecificationContent> specificationContents) {
		List<ModelMap> list=new ArrayList<ModelMap>();
		for(SpecificationContent sc:specificationContents){
			list.add(builderModelAll(sc));
		}
		return list;
	}

	public ModelMap builderModelAll(SpecificationContent sc) {
		ModelMap map=new ModelMap();
		Specification specification=specificationService.findEntityById(sc.getSpecificationId());
		if(specification==null)
			CodeContent.returnFailure("/规格获取失败！");
		User user=userService.findEntityById(sc.getCreateUserId());
		map.put("id",sc.getId());
		map.put("specificationName",specification.getName());
		map.put("content",sc.getContent());
		map.put("createTime", Convert.toFullYMDHMS(sc.getCreateTime()));
		map.put("isValid",sc.getIsValid()==true?"有效":"无效");
		map.put("createUserName",user.getRealName()==null?user.getName():user.getRealName());
		return map;
	}

	public Map<Integer,List<SpecificationContent>> getAllValidSpecification(Integer goodid) {
		if(goodid <= 0)
			return null;

		SpecificationContent param = new SpecificationContent();
		param.setGoodId(goodid);
		param.setIsValid(true);
		List<SpecificationContent> goodsSpecifications = getList(-1, -1, param);

		//所包含的分类
		List<Integer> specificationIds = new ArrayList<Integer>();
		for (SpecificationContent goodsSpecification:goodsSpecifications){
			Specification specification=specificationService.findEntityById(goodsSpecification.getSpecificationId());
			if(specification.getIsValid()==true){
				if(!specificationIds.contains(goodsSpecification.getSpecificationId()))
					specificationIds.add(goodsSpecification.getSpecificationId());
			}
		}

		//分类结果集
		Map<Integer, List<SpecificationContent>> map = new HashMap<Integer, List<SpecificationContent>>();
		for (Integer specificationId:specificationIds){
			List<SpecificationContent> list = new ArrayList<SpecificationContent>();
			for (SpecificationContent goodsSpecification:goodsSpecifications){
				if(specificationId.equals(goodsSpecification.getSpecificationId()))
					list.add(goodsSpecification);
			}
			map.put(specificationId, list);
		}
		return map;
	}

    public void addSpecification(User user, String goodid, String specificationName1, String specifcationContentName1) {
		Specification specification1=new Specification();
		specification1.setIsValid(true);
		specification1.setName(specificationName1);
		specification1.setGoodId(Integer.parseInt(goodid));
		specification1.setCreateUserId(user.getId());
		specification1.setCreateTime(new Date());
		specificationService.addEntity(specification1);

		String[] specificationContent1s=specifcationContentName1.split(",");
		for (String s:specificationContent1s){
			SpecificationContent specificationContent1=new SpecificationContent();
			specificationContent1.setGoodId(Integer.parseInt(goodid));
			specificationContent1.setContent(s);
			specificationContent1.setIsValid(true);
			specificationContent1.setCreateUserId(user.getId());
			specificationContent1.setCreateTime(new Date());
			specificationContent1.setSpecificationId(specification1.getId());
			specificationContentService.addEntity(specificationContent1);
		}
    }
}